﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfSnqkGasAnalysis.IBusiness_impl;
using WpfSnqkGasAnalysis.Model;

namespace WpfSnqkGasAnalysis
{
    /// <summary>
    /// 特殊气。分页
    /// </summary>
    /// 创建时间：2022-12-15 15:17:12
    public partial class PageTeshuqi : Page
    {
        public PageTeshuqi()
        {
            InitializeComponent();
        }
        TeshuBusiness teshuBusiness = new TeshuBusiness();
        SourceDictBusiness sourceDictBusiness = new SourceDictBusiness();

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            //步骤查询条件  //创建时间：2023-1-16 14:44:50
            cmb_Step.ItemsSource = CommonConfig.StepList;
            cmb_Step.DisplayMemberPath = "Value";
            cmb_Step.SelectedValuePath = "Key";

            LoadPageData(1);
        }

        /// <summary>
        /// 查询分页
        /// </summary>
        /// <param name="p"></param>
        private void LoadPageData(int p)
        {
            Teshu_gas teshu_Gas = new Teshu_gas();
            teshu_Gas.Report_no = this.txtReportNo.Text.Trim();

            //步骤查询，//创建时间：2023-1-16 17:50:20。
            if (cmb_Step.SelectedValue != null && cmb_Step.SelectedValue?.ToString() != "-1")
            {
                teshu_Gas.O_step = (byte)cmb_Step.SelectedValue.ToMyInt64T2();
            }

            Task.Run(() =>
            {
                var page = teshuBusiness.Page(teshu_Gas, p, CommonConfig.PageSize);

                this.Dispatcher.Invoke(() =>
               {
                   this.dataGrid.DataContext = page.List;
                   this.pager.PageIndex = p;
                   this.pager.PageSize = page.PageSize;
                   this.pager.Total = page.Total;
               });
            });
        }

        //查询   //创建时间：2022-12-15 15:58:13
        private void search_Click(object sender, RoutedEventArgs e)
        {
            LoadPageData(1);
        }

        //录入
        private void input_Click(object sender, RoutedEventArgs e)
        {
            WindowTeshuInput windowTeshuInput = new WindowTeshuInput();
            windowTeshuInput.RefreshPageList += WindowTeshuInput_RefreshPageList;
            windowTeshuInput.ShowDialog();
        }

        private void WindowTeshuInput_RefreshPageList(object? sender, EventArgs e)
        {
            LoadPageData(this.pager.PageIndex);
        }

        //编辑
        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            var ts = this.dataGrid.SelectedItem as Teshu_gas;

            WindowTeshuGasEdit windowTesh = new WindowTeshuGasEdit();
            windowTesh.TeshuGas = ts;
            windowTesh.TeshuGas.E12 = 77;//标记为编辑
            windowTesh.RefreshPageList += WindowTesh_RefreshPageList;
            windowTesh.ShowDialog();
        }

        private void WindowTesh_RefreshPageList(object? sender, EventArgs e)
        {
            LoadPageData(pager.PageIndex);
        }

        //打印
        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            //选中的数据
            Teshu_gas? gas = this.dataGrid.SelectedItem as Teshu_gas;

            //读取文本实体
            var info = teshuBusiness.GetView(gas.Id);

            //备注 ，标准。创建时间：2023-1-13 10:36:37
            var biaozhunList = sourceDictBusiness.GetListByGroup("teshu_report_remark", 1);
            biaozhunList = biaozhunList.OrderBy(g => g.Createtime).ToList();
            info.source_Dicts = biaozhunList;

            WindowProofreadPrintPreview ppv = new WindowProofreadPrintPreview("FlowDocumentTeshu.xaml", info, new PrintGas_analysisDocument());

            ppv.ShowInTaskbar = false;
            ppv.Show();
        }

        //审核
        private void check_Click(object sender, RoutedEventArgs e)
        {
            var ts = this.dataGrid.SelectedItem as Teshu_gas;
            WindowTeshuGasCheck win = new WindowTeshuGasCheck();
            win.TeshuGas = ts;
            win.RefreshPageList += WindowTesh_RefreshPageList;
            win.ShowDialog();
        }

        //日志
        private void log_Click(object sender, RoutedEventArgs e)
        {
            var ts = this.dataGrid.SelectedItem as Teshu_gas;
            WindowTeshuLog windowTeshuLog = new WindowTeshuLog();
            windowTeshuLog.teshu_Gas = ts;
            windowTeshuLog.ShowDialog();
        }

        //校对
        private void proo_Click(object sender, RoutedEventArgs e)
        {
            var ts = this.dataGrid.SelectedItem as Teshu_gas;

            WindowTeshuGasEdit edit = new WindowTeshuGasEdit();
            edit.TeshuGas = ts;
            edit.TeshuGas.E12 = 226;//标记为校对
            edit.RefreshPageList += WindowTesh_RefreshPageList;
            edit.ShowDialog();
        }

        //删除
        private void del_Click(object sender, RoutedEventArgs e)
        {
            var ts = this.dataGrid.SelectedItem as Teshu_gas;
            var resultOp = MessageBox.Show($"确认删除报告编号【{ts.Report_no}】吗？", "操作提示", MessageBoxButton.YesNo);
            if (resultOp != MessageBoxResult.Yes)
            {
                e.Handled = true;
                return;
            }
            var result = teshuBusiness.Delete(ts.Id);
            ShowAlertHelpter.ShowAlert(result.Msg);
            LoadPageData(pager.PageIndex);
        }

        //点击页码
        private void PageBar_PageIndexChanged(object sender, RoutedPropertyChangedEventArgs<int> e)
        {
            LoadPageData(pager.PageIndex);
        }
    }
}
